package com.vstartek.dm.mongo.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import com.vstartek.dm.mongo.dao.VideoDataDao;
import com.vstartek.dm.mongo.model.VideoData;
import com.vstartek.dm.mongo.service.VideoDataService;
import com.vstartek.dm.util.Page;
import com.vstartek.dm.util.PageParam;
import com.vstartek.dm.util.SQLUtil;
import com.vstartek.dm.util.TimeUtil;
@Service
public class VideoDataServiceImpl implements VideoDataService {

	@Autowired
	VideoDataDao videoDataDao;
	
	@Override
	public Page<VideoData> findByPage(PageParam pageParam, VideoData videoData) {
		
		Query query = new Query();
		if(videoData.getWebUrl()!=null)
		query.addCriteria(new Criteria().orOperator(Criteria.where("web_url").regex(".*?" + videoData.getWebUrl() + ".*"),Criteria.where("webSite_en").regex(".*?" + videoData.getWebUrl() + ".*")));
		
		SQLUtil.dealTimeWhere("updateTime", query, pageParam);
		
		Long num = videoDataDao.totalnum(videoData,"video_data_2014");
		List<VideoData> vlist = videoDataDao.findByList(query,pageParam,"video_data_2014");
		Page<VideoData> page = new Page<VideoData>(vlist,num,pageParam);
		return page;
	}

	@Override
	public VideoData saveVideoData(VideoData videoData) {
		videoData.setUpdateTime(TimeUtil.getNowSTime());
		videoData.setLastUpdateTime(TimeUtil.getNowSTime());
		if(videoDataDao.save(videoData,"video_data_2014")){
			return videoData;
		}
		return null;
	}

	@Override
	public boolean updateVideoData(VideoData videoData) {
		videoData.setLastUpdateTime(TimeUtil.getNowSTime());
		return videoDataDao.update(videoData,"video_data_2014");
	}

	@Override
	public VideoData findById(Object id) {
		return videoDataDao.findById(id,"video_data_2014");
	}

}
